package com.excel;

import java.util.ArrayList;
import java.util.List;


public class ComplexColumn extends ExcelColumn {

    /**
     * 需要合并的列名,顺序决定了在Excel中的显示位置
     */
    private List<String> columnNames = new ArrayList<String>();
//    private String[] columnDisplayNames;
    
   private List<ExcelColumn> fields = new ArrayList<ExcelColumn>();

//    /**
//     * 合并列
//     */
//    public void mergeColumn(String mergedName, String[] columnDisplayNames,
//	    String[] columnNames) {
//	this.displayName = mergedName;
//	this.columnNames = columnNames;
//	this.columnDisplayNames = columnDisplayNames;
//
//    }

   protected  List<String> getColumnNames() {
       return this.columnNames;
   }
   
    /**
     * 通过字符串格式合并列
     * 
     * @param formatNames
     *                格式为：mergedName(columnDisplay1:columnField1,columnDisplay2:columnField2)
     */
    public void mergeColumn(String formatNames) {
	int leftFlag = formatNames.indexOf(Constant.HEADFORMAT_SPRACELEFT_FLAG);
	int rightFlag = formatNames
		.lastIndexOf(Constant.HEADFORMAT_SPRACERIGHT_FLAG);
	String columns = formatNames.substring(leftFlag + 1, rightFlag);
	this.displayName = formatNames.substring(0, leftFlag);
	
	String[] colStrings = columns.split(Constant.SPLIT_FLAG);
	for (int i = 0; i < colStrings.length; i++) {	   
	    int aIndex = colStrings[i].indexOf(Constant.FIELD_DISPLAY_SPLIT_FLAG);
	    String displayName = colStrings[i].substring(0,aIndex);
	    String fieldName = colStrings[i].substring(aIndex+1);	    
	    SimpleExcelColumn simpleExcelColumn = new SimpleExcelColumn();
	    simpleExcelColumn.displayName = displayName;
	    simpleExcelColumn.setColumnName(fieldName);
	    fields.add(simpleExcelColumn);
	    columnNames.add(fieldName);
	}
    }

    @Override
    protected List<ExcelColumn> getColumns() {
	return this.fields;
    }

  

}
